package com.coffee.soft.controller;

import com.alibaba.fastjson.JSON;
import com.coffee.soft.service.impl.QiNiuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author:LiuRuidong
 * @Description: 七牛云上传图片
 * @Date: Created in 21:26 2018/2/6
 * @Modified By:
 */
@Controller
public class QiNiuUpload {

    @ResponseBody
    @RequestMapping("/uploadHandler")
    public void uploadHandler(@RequestParam("uploadFile")MultipartFile uploadFile,HttpServletRequest request,HttpServletResponse response) {
        String result = null;
        try {
            String callback = request.getParameter("callback");

            result = new QiNiuService().upload(uploadFile);

            System.out.println(result);
            String json = JSON.toJSONString(result);
            try {
                PrintWriter pw = response.getWriter();
                response.setCharacterEncoding("utf-8");
                response.setContentType("text/html;charset=utf-8");
                if ((callback != "") && (callback != null)) {
                    json = callback + "(" + json + ")";
                }
                pw.println(json);
                pw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


        @RequestMapping("/upload")
        public String upload(){
            return "login";
        }

}
